package cn.iocoder.yudao.module.psy.service.resource;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.psy.controller.admin.resource.vo.*;
import cn.iocoder.yudao.module.psy.dal.dataobject.resource.ResourceDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.psy.dal.mysql.resource.ResourceMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 心理评估资源 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ResourceServiceImpl implements ResourceService {

    @Resource
    private ResourceMapper resourceMapper;

    @Override
    public Long createResource(ResourceSaveReqVO createReqVO) {
        // 插入
        ResourceDO resource = BeanUtils.toBean(createReqVO, ResourceDO.class);
        resourceMapper.insert(resource);
        // 返回
        return resource.getId();
    }

    @Override
    public void updateResource(ResourceSaveReqVO updateReqVO) {
        // 校验存在
        validateResourceExists(updateReqVO.getId());
        // 更新
        ResourceDO updateObj = BeanUtils.toBean(updateReqVO, ResourceDO.class);
        resourceMapper.updateById(updateObj);
    }

    @Override
    public void deleteResource(Long id) {
        // 校验存在
        validateResourceExists(id);
        // 删除
        resourceMapper.deleteById(id);
    }

    private void validateResourceExists(Long id) {
        if (resourceMapper.selectById(id) == null) {
//            throw exception(RESOURCE_NOT_EXISTS);
        }
    }

    @Override
    public ResourceDO getResource(Long id) {
        return resourceMapper.selectById(id);
    }

    @Override
    public PageResult<ResourceDO> getResourcePage(ResourcePageReqVO pageReqVO) {
        return resourceMapper.selectPage(pageReqVO);
    }

}